module.exports =  class KoaRouter {
    constructor() {
        this.callbacks= [];
    }
    get(path, callback) {
        this.callbacks.push({
            path,
            route: callback
        })
    }
    routes() {
        return async (ctx, next) => {
            try {
                const path = ctx.path;
                const routes = this.callbacks.filter(route => route.path === path);
                this.compose(routes, next, ctx);
            } catch(e) {
                next();
            }
        }
    }
    compose(routes, next, ctx) {
        let index = 0;
        function dispatch(i) {
            // 获取一个个匹配到的路由
            if (index === routes.length) return next();
            const route = routes[i];
            route.route(ctx, () => {dispatch + 1});
        }
        dispatch(index);
    }
}